﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
namespace WebsiteBanThucAn.DAL
{
    public class SqlDataProvider
    {
        public static string chuoiKetNoi = @"Data Source=.\SQLEXPRESS;Initial Catalog=HappyFastFood;Integrated Security=SSPI;";
        private static SqlConnection ketNoi;
        public static DataTable DocBang(string lenh)
        {//Select
            DataTable bang = new DataTable();
            try
            {
                if (ketNoi == null)
                {
                    ketNoi = new SqlConnection(chuoiKetNoi);
                }
                SqlDataAdapter da = new SqlDataAdapter(lenh, ketNoi);
                da.FillSchema(bang, SchemaType.Mapped);
                da.Fill(bang);
                da.Dispose();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                ketNoi.Close();
                bang.Dispose();
            }
            return bang;
        }
        public static int ThucHienLenh(string lenh)
        {//Insert,Update,Delete
            int kq = -1;
            try
            {
                if (ketNoi == null)
                {
                    ketNoi = new SqlConnection(chuoiKetNoi);
                }
                if (ketNoi.State == ConnectionState.Closed)
                {
                    ketNoi.Open();
                }
                SqlCommand cmd = new SqlCommand(lenh, ketNoi);
                kq = cmd.ExecuteNonQuery();
                cmd.Dispose();
            }
            catch (SqlException ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                ketNoi.Close();
            }
            return kq;
        }
    }
}